package com.jet.chat.client;

import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.io.PrintWriter;
import java.net.ServerSocket;
import java.net.Socket;
import java.net.UnknownHostException;

import com.jet.chat.exception.ChatExceptionClientReaderNotAvailable;
import com.jet.chat.exception.ChatExceptionClientTwistedServerStartFailed;
import com.jet.chat.exception.ChatExceptionClientWriterNotAvailable;
import com.jet.chat.exception.ChatExceptionServerHostNotAvailable;
import com.jet.chat.message.MessageReaderObjectInputStream;
import com.jet.chat.message.MessageWriterObjectOutputStream;
import com.jet.chat.message.MessageWriterPrintWriter;
import com.jet.chat.service.Arguments;
import com.jet.chat.thread.ChatThread;

public class ChatClientTwistedReader {
	private Socket chatSocket;
	private ObjectInputStream chatReader;
	private ObjectOutputStream chatWriter;
	
	private ServerSocket clientServerSocket;

	public ChatClientTwistedReader(String chatHost, int chatPort, int clientPort) throws ChatExceptionServerHostNotAvailable, ChatExceptionClientReaderNotAvailable, ChatExceptionClientWriterNotAvailable, ChatExceptionClientTwistedServerStartFailed {
		try {
			this.chatSocket = new Socket(chatHost, chatPort);
		} catch (UnknownHostException e) {
			throw new ChatExceptionServerHostNotAvailable("host not found");
		} catch (IOException e) {
			throw new ChatExceptionServerHostNotAvailable("ololo netnet net");
		}

		try {
			this.chatWriter = new ObjectOutputStream(chatSocket.getOutputStream());
		} catch (IOException e) {
			throw new ChatExceptionClientWriterNotAvailable("ошибка создания потока вывода клиента");
		}
		
		try {
			this.chatReader = new ObjectInputStream(chatSocket.getInputStream());
		} catch (IOException e) {
			throw new ChatExceptionClientReaderNotAvailable("ошибка создания потока чтения клиента");
		}
		
		try {
			this.clientServerSocket = new ServerSocket(clientPort);
		} catch (IOException e) {
			throw new ChatExceptionClientTwistedServerStartFailed("twisted client server port not available");
		}
	}
	
	public void execute() {
		try {
			Socket clientSocket = clientServerSocket.accept();
			System.out.println("client writer socket accepted");

			new Thread(new ChatThread(new MessageReaderObjectInputStream(new ObjectInputStream(clientSocket.getInputStream())), new MessageWriterObjectOutputStream(chatWriter), "client server shutdown")).start();
			//new Thread(new ChatThread(new MessageReaderObjectInputStream(chatReader), new MessageWriterPrintWriter(new PrintWriter(System.out)), "server shutdown")).start();
			new Thread(
					new ChatClientReaderThread(
							new MessageReaderObjectInputStream(chatReader),
							new MessageWriterPrintWriter(new PrintWriter(System.out)),
							new MessageWriterObjectOutputStream(chatWriter)
					)
			).start();
			
			System.out.println("ChatClientTwistedReader started");

		} catch (IOException e) {
			System.out.println("что-то не так");
			//e.printStackTrace();
		}
		
	}

	public static void main(String[] arg) {
		ChatClientTwistedReader chatClientTwistedReader;
		try {
			String hostname = "localhost";
			int port = 30030;
			
			int clientServerPort = 30300;

			Arguments arguments = new Arguments(arg);

			String clientServerPortArg = arguments.getValue("clientServerPort");
			if (clientServerPortArg != null && !clientServerPortArg.equals("")) {
				clientServerPort = Integer.parseInt(clientServerPortArg);
			}

			String hostnameArg = arguments.getValue("hostname");
			if (hostnameArg != null && !hostnameArg.equals("")) {
				hostname = hostnameArg;
			}
			
			System.out.println("clientServerPort: " + clientServerPort);

			chatClientTwistedReader = new ChatClientTwistedReader(hostname, port, clientServerPort);
			chatClientTwistedReader.execute();
		} catch (ChatExceptionServerHostNotAvailable e) {
			System.out.println("Сервер недоступен");
			 //[ek] TODO need more user-friendly messages
		} catch (ChatExceptionClientReaderNotAvailable e) {
			System.out.println("Ошибка потока чтения клиента");
		} catch (ChatExceptionClientWriterNotAvailable e) {
			System.out.println("Ошибка потока вывода клиента");
		} catch (ChatExceptionClientTwistedServerStartFailed e) {
			System.out.println("twisted client server port not available");
		}
	}
}
